/**
 * 
 */
package edu.arizona.bio5.onto.decomposition.persistent.xml;

import java.util.HashSet;
import java.util.Set;

import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLEntity;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import edu.arizona.bio5.onto.decomposition.Label;
import edu.arizona.bio5.onto.decomposition.MSSLabel;
import edu.arizona.bio5.onto.decomposition.persistent.LabelLoader;
import edu.arizona.bio5.onto.util.OntologyUtils;

/**
 * @author Pavel Klinov
 *
 */
public class XMLSeedSigLabelLoader implements LabelLoader<Element> {

	private Element mElement = null;
	private OWLDataFactory mFactory = null;
	
	public XMLSeedSigLabelLoader(OWLDataFactory factory) {
		mFactory = factory;
	}
	
	@Override
	public void setInput(Element input) {
		mElement = input;
	}

	@Override
	public Label load() {
		// Parse the document and load min seed signatures
		if (mElement != null && mElement.getNodeName().equals(XMLADConstants.LABEL_ELEM)) {
			String complete = mElement.getAttribute(XMLADConstants.COMPLETE_LABEL_ATTRIBUTE);
			NodeList sigElems = mElement.getElementsByTagName(XMLADConstants.SIGNATURE_ELEM);
			Set<Set<OWLEntity>> mss = new HashSet<Set<OWLEntity>>();
			
			for (int i = 0; i < sigElems.getLength(); i++) {
				Set<OWLEntity> sig = new HashSet<OWLEntity>();
				Element sigElem = (Element)sigElems.item(i);
				NodeList termElems = sigElem.getElementsByTagName(XMLADConstants.TERM_ELEM);
				
				for (int j = 0; j < termElems.getLength(); j++) {
					Element termElem = (Element)termElems.item(j);
					OWLEntity term = OntologyUtils.termFromString(mFactory, termElem.getTextContent());
					
					sig.add(term);
				}
				
				mss.add(sig);
			}
			
			return new MSSLabel(mss, Boolean.valueOf(complete));
		}
		
		return null;
	}
}